package TUILayer;
import ControlLayer.*;

public class AddressBookUI
{

    private AddressBookCtr addressBookCtr;
    private InputParser inputParser;
    public AddressBookUI(InputParser inputParser)
    {
        addressBookCtr = new AddressBookCtr();
        this.inputParser = inputParser;
    }

    public void start()
    {
        int choise = 0;
        boolean exit = false;
        while(choise < 1 || choise > 7 || !exit)
        {
            choise = inputParser.getInt("\f ***AddressBookMenu ***\n\n1. Show information about person\n2. Show a person's loans\n3. Add person\n4. Delete person\n5. Update person\n6. List all persons\n7. Return\n\nMake your choise: ");
            if(choise == 1)
            {
                showInfo();
            }
            if(choise == 2)
            {
                showLoans();
            }
            else if(choise == 3)
            {
                createPerson();
            }
            else if(choise == 4)
            {
                deletePerson();
            }
            else if(choise == 5)
            {
                updatePerson();
            }
            else if(choise == 6)
            {
                listAllPersons();
            }
            else if(choise == 7)
            {
                exit = true;
            }

        }
    }
    
    private void showLoans()
    {
        int id = inputParser.getInt("\f ***ShowPersonLoansMenu ***\n\nPlease input the id of the person: ");
        System.out.println("\f ***ShowPersonLoansMenu ***\n");
        if(addressBookCtr.getPerson(id) != null)
        {
            addressBookCtr.printPersonLoanList(id);
        }
        else
        {
            System.out.println("Error: Person not found");
        }
        inputParser.printReturn();
    }

    private void createPerson()
    {
        int id = inputParser.getInt("\f ***CreatePersonMenu ***\n\nPlease input the id of the new person: ");
        if(addressBookCtr.getPerson(id) == null)
        {
            String name = inputParser.getString("\f ***CreatePersonMenu ***\n\nPlease input the name of the new person: ");
            String address = inputParser.getString("\f ***CreatePersonMenu ***\n\nPlease input the address of the new person: ");
            long postalCode = inputParser.getLong("\f ***CreatePersonMenu ***\n\nPlease input the postal code of the new person: ");
            String city = inputParser.getString("\f ***CreatePersonMenu ***\n\nPlease input the city of the new person: ");
            long phoneNr = inputParser.getLong("\f ***CreatePersonMenu ***\n\nPlease input the phone number of the new person: ");
            addressBookCtr.createPerson(id, name, address, postalCode, city, phoneNr);
            System.out.println("\f ***CreatePersonMenu ***\n\nPerson added.");
        }
        else
        {
            System.out.println("\f ***CreatePersonMenu ***\n\nError: Id already exists in the AddressBook");
        }
        inputParser.printReturn();
    }

    private void showInfo()
    {        
        int id = inputParser.getInt("\f ***ShowPersonInfoMenu ***\n\nPlease input the id of the person: ");
        System.out.println("\f ***ShowPersonInfoMenu ***\n");
        if(addressBookCtr.getPerson(id) != null)
        {
            addressBookCtr.printPerson(id);
        }
        else
        {
            System.out.println("Error: Person not found");
        }
        inputParser.printReturn();
    }

    private void deletePerson()
    {
        int id = inputParser.getInt("\f ***DeletePersonMenu ***\n\nPlease input the id of the person: ");
        System.out.println("\f ***DeletePersonMenu ***\n");
        if(addressBookCtr.getPerson(id) != null)
        {
            addressBookCtr.removePerson(id);
            System.out.println("Person deleted.");
        }
        else
        {
            System.out.println("Error: Person not found");
        }
        inputParser.printReturn();
    }

    private void listAllPersons()
    {
        System.out.println("\f ***ListAllPersonMenu ***\n");
        addressBookCtr.listAllPersons();
        inputParser.printReturn();
    }

    private void updatePerson()
    {
        int id = inputParser.getInt("\f ***UpdatePersonMenu ***\n\nPlease input the id of the person: ");
        if(addressBookCtr.getPerson(id) != null)
        {
            int choise = 0;
            String value = "";
            boolean exit = false;
            while(choise > 0 && choise < 7 || !exit)
            {
                choise = inputParser.getInt("\f ***UpdatePersonMenu ***\n\n1. Edit name\n2. Edit address\n3. Edit postal code\n4. Edit city\n5. Edit phone no\n6. Return\n\nMake your choise: ");
                if(choise > 0 && choise < 7)
                {
                    if(choise == 6)
                    {
                        choise = 7;
                        exit = true;
                    }
                    else
                    {
                        if(choise == 1)
                        {
                            value = inputParser.getString("\f ***UpdatePersonMenu ***\n\nPlease input the new name of the person: ");
                        }
                        else if(choise == 2)
                        {
                            value = inputParser.getString("\f ***UpdatePersonMenu ***\n\nPlease input the new address of the person: ");
                        }
                        else if(choise == 3)
                        {
                            value = String.valueOf(inputParser.getLong("\f ***UpdatePersonMenu ***\n\nPlease input the new postal code of the person: "));
                        }
                        else if(choise == 4)
                        {
                            value = inputParser.getString("\f ***UpdatePersonMenu ***\n\nPlease input the new city of the person: ");
                        }
                        else if(choise == 5)
                        {
                            value = String.valueOf(inputParser.getLong("\f ***UpdatePersonMenu ***\n\nPlease input the new phone no of the person: "));
                        }
                        addressBookCtr.updatePerson(choise, id, value);
                        System.out.println("\f ***UpdatePersonMenu ***\n\nPerson updated");
                        inputParser.printReturn();
                    }
                }
            }
        }
        else
        {
            System.out.println("\f ***UpdatePersonMenu ***\n\nError: Person not found");
            inputParser.printReturn();
        }
    } 


}
